package com.osmino.contacts.entity;

import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.osmino.contacts.PreferenceKeys;

public final class QueryRestrictions {

	private static final int IS_SIM = 0;
	private static final int IS_GOOGLE = 1;
	private static final int IS_FACEBOOK = 2;
	private static final int IS_TWITTER = 3;
	private static final Map<String, Integer> sTypeMap;

	static {
		sTypeMap = new HashMap<String, Integer>();
		sTypeMap.put(LocalAccount.SIM_ACCOUNT_TYPE, IS_SIM);
		sTypeMap.put(LocalAccount.GOOGLE_ACCOUNT_TYPE, IS_GOOGLE);
		sTypeMap.put(LocalAccount.FACEBOOK_ACCOUNT_TYPE, IS_FACEBOOK);
		sTypeMap.put(LocalAccount.TWITTER_ACCOUNT_TYPE, IS_TWITTER);
	}

	public boolean simEnabled;
	public boolean phoneEnabled;
	public boolean googleEnabled;
	public boolean facebookEnabled;
	public boolean twitterEnabled;
	public boolean withNumbersOnly;

	public QueryRestrictions(Context context) {
		SharedPreferences applicationPreferences = PreferenceManager.getDefaultSharedPreferences(context);
		phoneEnabled = applicationPreferences.getBoolean(PreferenceKeys.PREF_SHOW_PHONE_CONTACTS, true);
		googleEnabled = applicationPreferences.getBoolean(PreferenceKeys.PREF_SHOW_GOOGLE_CONTACTS, true);
		simEnabled = applicationPreferences.getBoolean(PreferenceKeys.PREF_SHOW_SIM_CONTACTS, true);
		facebookEnabled = applicationPreferences.getBoolean(PreferenceKeys.PREF_SHOW_FACEBOOK_CONTACTS, true);
		twitterEnabled = applicationPreferences.getBoolean(PreferenceKeys.PREF_SHOW_TWITTER_CONTACTS, true);
		withNumbersOnly = applicationPreferences.getBoolean(PreferenceKeys.PREF_SHOW_WITH_NUMBERS_ONLY, false);
	}

	public boolean isAccountTypeEnabled(String accountType) {
		Integer type = sTypeMap.get(accountType);
		boolean result = false;
		if (type == null) {
			result = phoneEnabled;
		} else if (type == IS_SIM) {
			result = simEnabled;
		} else if (type == IS_GOOGLE) {
			result = googleEnabled;
		} else if (type == IS_FACEBOOK) {
			result = facebookEnabled;
		} else if (type == IS_TWITTER) {
			result = twitterEnabled;
		} else {
			result = phoneEnabled;
		}
		return result;
	}

	public boolean isContactEnabled(Contact contact) {
		return contact.hasPhoneNumber || !withNumbersOnly;
	}
}
